package com.github.lzyzsd.circleprogress;

import com.github.lzyzsd.circleprogress.utils.RectF;
import com.github.lzyzsd.circleprogress.utils.TextUtils;
import com.github.lzyzsd.circleprogress.utils.properties.CircleProgressResPropertyHolder;
import com.github.lzyzsd.circleprogress.utils.properties.model.CircleProgressModel;
import com.github.lzyzsd.circleprogress.utils.properties.IInterface.IPropertyHolder;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * Created by bruce on 11/4/14.
 */
public final class CircleProgress extends Component implements Component.DrawTask {
    private String TAG = CircleProgress.class.getSimpleName();
    private Paint textPaint;
    private RectF rectF = new RectF();
    private IPropertyHolder<CircleProgressModel> propertyHolder;
    private int textSize;
    private Color textColor;
    private int progress = 0;
    private String text;
    private int max;
    private Color finishedColor;
    private Color unfinishedColor;
    private String prefixText = "";
    private String suffixText = "%";

    private static final String INSTANCE_STATE = "saved_instance";
    private static final String INSTANCE_TEXT_COLOR = "text_color";
    private static final String INSTANCE_TEXT_SIZE = "text_size";
    private static final String INSTANCE_FINISHED_STROKE_COLOR = "finished_stroke_color";
    private static final String INSTANCE_UNFINISHED_STROKE_COLOR = "unfinished_stroke_color";
    private static final String INSTANCE_MAX = "max";
    private static final String INSTANCE_PROGRESS = "progress";
    private static final String INSTANCE_SUFFIX = "suffix";
    private static final String INSTANCE_PREFIX = "prefix";

    private Paint paint = new Paint();

    public CircleProgress(Context context) {
        this(context, null);
    }

    public CircleProgress(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public CircleProgress(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initByAttributes(context, attrs);
        initPainters();
        addDrawTask(this);
    }

    protected void initByAttributes(Context context, AttrSet attrSet) {
        propertyHolder = new CircleProgressResPropertyHolder(context, attrSet);
        CircleProgressModel circleProgressModel = propertyHolder.obtainProperty();
        setStyleWith(circleProgressModel);

        /*
        finishedColor = attributes.getColor(R.styleable.CircleProgress_circle_finished_color, default_finished_color);
        unfinishedColor = attributes.getColor(R.styleable.CircleProgress_circle_unfinished_color, default_unfinished_color);
        textColor = attributes.getColor(R.styleable.CircleProgress_circle_text_color, default_text_color);
        textSize = attributes.getDimension(R.styleable.CircleProgress_circle_text_size, default_text_size);

        setMax(attributes.getInt(R.styleable.CircleProgress_circle_max, default_max));
        setProgress(attributes.getInt(R.styleable.CircleProgress_circle_progress, 0));

        if (attributes.getString(R.styleable.CircleProgress_circle_prefix_text) != null) {
            setPrefixText(attributes.getString(R.styleable.CircleProgress_circle_prefix_text));
        }
        if (attributes.getString(R.styleable.CircleProgress_circle_suffix_text) != null) {
            setSuffixText(attributes.getString(R.styleable.CircleProgress_circle_suffix_text));
        }
         */
    }

    public void setStyleWith(CircleProgressModel circleProgressModel) {
        finishedColor = circleProgressModel.getFinishedColor();
        unfinishedColor = circleProgressModel.getUnfinishedColor();
        textColor = circleProgressModel.getTextColor();
        textSize = circleProgressModel.getTextSize();
        progress = circleProgressModel.getProgress();
        max = circleProgressModel.getMax();

        String prefixText = circleProgressModel.getPrefixText();
        if (!TextUtils.isEmpty(prefixText)) {
            setPrefixText(prefixText);
        }

        String suffixText = circleProgressModel.getSuffixText();
        if (!TextUtils.isEmpty(suffixText)) {
            setSuffixText(suffixText);
        }
    }


    /*
    protected void initWithAttribute() {
        finishedColor =  default_finished_color;
        unfinishedColor = default_unfinished_color;
        textColor = default_text_color;
        textSize =  default_text_size;
        setMax(default_max);
        setProgress( 0);
    }
     */

    protected void initPainters() {
        textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);

        paint.setAntiAlias(true);
    }

    @Override
    public void invalidate() {
        initPainters();
        super.invalidate();
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
        if (this.progress > getMax()) {
            this.progress %= getMax();
        }
        invalidate();
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        if (max > 0) {
            this.max = max;
            invalidate();
        }
    }

    public void setDefaultText() {
        text = null;
        invalidate();
    }

    /**
     * Setting Central Text to custom String
     *
     * @param text 需要设置的文本
     */
    public void setText(String text) {
        this.text = text;
        invalidate();
    }

    public String getText() {
        if (text != null) {
            return text;
        } else return String.valueOf(progress);
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        this.invalidate();
    }

    public Color getTextColor() {
        return textColor;
    }

    public void setTextColor(Color textColor) {
        this.textColor = textColor;
        this.invalidate();
    }

    public Color getFinishedColor() {
        return finishedColor;
    }

    public void setFinishedColor(Color finishedColor) {
        this.finishedColor = finishedColor;
        this.invalidate();
    }

    public Color getUnfinishedColor() {
        return unfinishedColor;
    }

    public void setUnfinishedColor(Color unfinishedColor) {
        this.unfinishedColor = unfinishedColor;
        this.invalidate();
    }

    public String getPrefixText() {
        return prefixText;
    }

    public void setPrefixText(String prefixText) {
        this.prefixText = prefixText;
        this.invalidate();
    }

    public String getSuffixText() {
        return suffixText;
    }

    public void setSuffixText(String suffixText) {
        this.suffixText = suffixText;
        this.invalidate();
    }

    public String getDrawText() {
        return getPrefixText() + getText() + getSuffixText();
    }

    /*
    @Override
    protected int getSuggestedMinimumHeight() {
        return min_size;
    }

    @Override
    protected int getSuggestedMinimumWidth() {
        return min_size;
    }
     */

    public float getProgressPercentage() {
        return getProgress() / (float) getMax();
    }

    /*
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        rectF.set(0, 0, MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }
     */
    private Arc mArc = new Arc();

    @Override
    public void onDraw(Component component, Canvas canvas) {
        rectF.set(0, 0, getWidth(), getHeight());

        float yHeight = getProgress() / (float) getMax() * getHeight();
        float radius = getWidth() / 2f;
        float angle = (float) (Math.acos((radius - yHeight) / radius) * 180 / Math.PI);
        float startAngle = 90 + angle;
        float sweepAngle = 360 - angle * 2;
        paint.setColor(getUnfinishedColor());
        mArc.setArc(startAngle, sweepAngle, false);
        canvas.drawArc(rectF, mArc, paint);

        //fixme we don't have 'canvas.save' ability on harmonyOS
        // canvas.save();
        canvas.rotate(180, getWidth() / 2, getHeight() / 2);
        paint.setColor(getFinishedColor());
        mArc.setArc(270 - angle, angle * 2, false);
        canvas.drawArc(rectF, mArc, paint);
        //fixme we rotate back the canvas manually
        // canvas.restore();
        canvas.rotate(-180, getWidth() / 2, getHeight() / 2);

        String text = getDrawText();
        if (!TextUtils.isEmpty(text)) {
            float textHeight = textPaint.getFontMetrics().descent + textPaint.getFontMetrics().ascent;
            canvas.drawText(textPaint, text, (getWidth() - textPaint.measureText(text)) / 2.0f, (getWidth() - textHeight) / 2.0f);
        }
    }

    /*
    @Override
    protected Parcelable onSaveInstanceState() {
        final Bundle bundle = new Bundle();
        bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
        bundle.putInt(INSTANCE_TEXT_COLOR, getTextColor());
        bundle.putFloat(INSTANCE_TEXT_SIZE, getTextSize());
        bundle.putInt(INSTANCE_FINISHED_STROKE_COLOR, getFinishedColor());
        bundle.putInt(INSTANCE_UNFINISHED_STROKE_COLOR, getUnfinishedColor());
        bundle.putInt(INSTANCE_MAX, getMax());
        bundle.putInt(INSTANCE_PROGRESS, getProgress());
        bundle.putString(INSTANCE_SUFFIX, getSuffixText());
        bundle.putString(INSTANCE_PREFIX, getPrefixText());
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if(state instanceof Bundle) {
            final Bundle bundle = (Bundle) state;
            textColor = bundle.getInt(INSTANCE_TEXT_COLOR);
            textSize = bundle.getFloat(INSTANCE_TEXT_SIZE);
            finishedColor = bundle.getInt(INSTANCE_FINISHED_STROKE_COLOR);
            unfinishedColor = bundle.getInt(INSTANCE_UNFINISHED_STROKE_COLOR);
            initPainters();
            setMax(bundle.getInt(INSTANCE_MAX));
            setProgress(bundle.getInt(INSTANCE_PROGRESS));
            prefixText = bundle.getString(INSTANCE_PREFIX);
            suffixText = bundle.getString(INSTANCE_SUFFIX);
            super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
            return;
        }
        super.onRestoreInstanceState(state);
    }
     */
}
